package com.guomei.service.curse.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guomei.bean.curse.CourseChapter;
import com.guomei.mapper.curse.CourseChapterMapper;
import com.guomei.service.curse.CourseChapterService;
import com.guomei.utils.PageData;
import com.guomei.utils.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 课程章节服务实现类
 */
@Service
@Transactional
public class CourseChapterServiceImpl implements CourseChapterService {
    @Resource
    private CourseChapterMapper courseChapterMapper;

    @Override
    public CourseChapter getById(Long id) {
        if (id == null) {
            return null;
        }
        return courseChapterMapper.selectById(id);
    }

    @Override
    public List<CourseChapter> getByCourseId(Long courseId) {
        if (courseId == null) {
            return null;
        }
        return courseChapterMapper.selectByCourseId(courseId);
    }

    @Override
    public List<CourseChapter> getAll() {
        return courseChapterMapper.selectAll();
    }

    @Override
    public Result<?> getByCondition(CourseChapter courseChapter) {
        PageHelper.startPage(courseChapter.getPageNum(), courseChapter.getPageSize());
        List<CourseChapter> courseChapters = courseChapterMapper.selectByCondition(courseChapter);
        // 封装分页结果到PageData
        PageData<CourseChapter> pageData = new PageData<>();
        PageInfo<CourseChapter> pageInfo = new PageInfo<>(courseChapters);
        pageData.setTotal(pageInfo.getTotal());
        pageData.setData(pageInfo.getList());
        pageData.setPageNum(courseChapter.getPageNum());
        pageData.setPageSize(courseChapter.getPageSize());
        return Result.success(pageData);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(CourseChapter courseChapter) {
        if (courseChapter == null || courseChapter.getCourseId() == null
                || courseChapter.getChapterName() == null || courseChapter.getChapterName().trim().isEmpty()) {
            return false;
        }

        LocalDateTime now = LocalDateTime.now();
        courseChapter.setCreatedTime(now);
        courseChapter.setUpdatedTime(now);

        if (courseChapter.getSortOrder() == null) {
            courseChapter.setSortOrder(0);
        }

        return courseChapterMapper.insert(courseChapter) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchSave(List<CourseChapter> chapters) {
        if (chapters == null || chapters.isEmpty()) {
            return false;
        }

        LocalDateTime now = LocalDateTime.now();
        for (CourseChapter chapter : chapters) {
            if (chapter.getCourseId() == null || chapter.getChapterName() == null || chapter.getChapterName().trim().isEmpty()) {
                return false;
            }

            chapter.setCreatedTime(now);
            chapter.setUpdatedTime(now);

            if (chapter.getSortOrder() == null) {
                chapter.setSortOrder(0);
            }
        }

        return courseChapterMapper.batchInsert(chapters) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(CourseChapter courseChapter) {
        if (courseChapter == null || courseChapter.getId() == null) {
            return false;
        }

        courseChapter.setUpdatedTime(LocalDateTime.now());
        return courseChapterMapper.updateById(courseChapter) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeById(Long id) {
        if (id == null) {
            return false;
        }
        return courseChapterMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchRemove(List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            return false;
        }
        return courseChapterMapper.batchDelete(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByCourseId(Long courseId) {
        if (courseId == null) {
            return false;
        }
        return courseChapterMapper.deleteByCourseId(courseId) > 0;
    }
}
